package org.nimi317.web_gis.utils;

import org.nimi317.web_gis.Enum.ProvinceDataEnum;

import java.time.LocalDateTime;

/**
 * 时间工具类主要用于提供包装函数用于简化时间操作s
 *
 * @author thunderobot
 */
public class TimeUtils {

    /**
     * 获取当前时间 系统毫秒
     *
     * @return 当前时间
     */
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间 系统纳秒
     *
     * @return 系统时间
     */
    public static LocalDateTime getCurrentDateTime() {
        return LocalDateTime.now();
    }

    /**
     * 获取当前可用时间 如果小于20时则为前一小时 大于等于则为当前小时
     *
     * @return 当前可用时间
     */
    public static LocalDateTime getCurrentUsefulDateTime() {
        LocalDateTime dateTime = getCurrentDateTime();
        LocalDateTime localDateTime = LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(), dateTime.getHour(), 0, 0);
        return dateTime.getMinute() < 20 ? localDateTime.minusHours(1) : localDateTime;
    }

    public static LocalDateTime getNextCurrentUsefulDateTime() {
        return getCurrentUsefulDateTime().plusHours(1);
    }

    public static LocalDateTime getDivisionDateTime(ProvinceDataEnum dataEnum) {
        LocalDateTime time;
        LocalDateTime dateTime = getCurrentUsefulDateTime();
        switch (dataEnum) {
            case Hours_6 -> time = dateTime.minusHours(5);
            case Hours_12 -> time = dateTime.minusHours(11);
            case Hours_24 -> time = dateTime.minusHours(23);
            case Month -> time = dateTime.minusDays(30);
            case Month_Half -> time = dateTime.minusDays(15);
            case Hours_8 -> time = dateTime.minusHours(7);
            default -> time = dateTime.minusHours(1);
        }
        return time;
    }
}
